<?php
namespace app\web\controller;

use app\model\Theme; 
use app\model\Sms; 

class Yxsms extends Base
{
    public function Yxsms()
    {
/**
 * Author: cbw
 * Desc: 易宣短信服务
 * Time: 2022/12/5 09:40
 * Email: 17854288794@163.com
 */
global $_G,$_S,$_C,$_P;
$themeid = intval($_P['themeid']);
if($themeid <= 0){
    $themeid = Theme::getDefaultThemenew();
}
$theme = Theme::getThemeById($themeid);
$config = 	Theme::getConfig($themeid);
$system = $_G['system'];
$do = 'yxsms';
$op = isset($_P['op'])?$_P['op']:'display';
$ac = isset($_P['ac'])?$_P['ac']:'';
$trade_status =- isset($_P['trade_status'])?$_P['trade_status']:'';

if (empty($_P['createtime']['start'])){
    $starttime = db_fetchcolumn ( 'SELECT min(addtime) FROM ' . tablename ( t_fans ) );
    $start_day =  date('Y-m-d H:i:s',$starttime);
}else{
    $starttime = strtotime($_P['createtime']['start']);
    $start_day = $_P['createtime']['start'];
}
if (empty($_P['createtime']['end'])){
    $endtime = time();
    $end_day = date("Y-m-d H:i:s",$endtime);
}else{
    $endtime = strtotime($_P['createtime']['end']);
    $end_day = $_P['createtime']['end'];
}




$page_index = max(1,intval($_P['page']));
$page_size = 50;
$page_start = ($page_index-1)*$page_size;
$type = isset($_P['type'])?$_P['type']:'';
$search_data = isset($_P['search_data'])?$_P['search_data']:'';
if ($search_data){
    $search_data = '%'.$search_data.'%';
}


$used_sms =Sms::getUsedSmsTotal($themeid);
$used_amount = max(0,0.1*$used_sms);

//发送日志
if ($op=='display'){
    $status = isset($_P['status'])?$_P['status']:0;
    $template_list  = Sms::getSmsTemplateCountByType($themeid,$system);
    $trade_status_list  = Sms::getTradeStatusCount($themeid);
    $status_list  = Sms::getStatusCount($themeid);

    $name_arr = getKeynameArray();
    $type_list = Sms::getSmsTemplateArr($system,'all',0,false,true);
    $page_total = Sms::getSmsLog($themeid,$search_data,0,0,$start_day,$end_day,$status,$type,true,$trade_status);
    $list =       Sms::getSmsLog($themeid,$search_data,$page_start,$page_size,$start_day,$end_day,$status,$type,false,$trade_status);
    $total = $page_total;
    //导出
    if (checksubmit('export_submit', true)){
        $count = $total;
        $pagesize = ceil($count/5000);
        $header = array(
            'mobile' => '手机','username' => '姓名',
            'nickname' => '微信昵称','type' => '场景',  'content'=>'短信内容','status' => '发送状态','trade_status'=>'送达状态',
            'created_at'=>'发送时间'
        );
        $keys = array_keys($header);
        $html = "\xEF\xBB\xBF";
        foreach ($header as $li) {
            $html .= $li . "\t ,";
        }
        $html .= "\n";
        for ($j = 1; $j <= $pagesize; $j++) {
            $page_start = ($j - 1) * 5000;
            $list = Sms::getSmsLog($themeid,$search_data,$page_start,5000,$start_day,$end_day,$status,$type,false,$trade_status);
            if (!empty($list)) {
                $size = ceil(count($list) / 500);
                for ($i = 0; $i < $size; $i++) {
                    $buffer = array_slice($list, $i * 500, 500);
                    foreach ($buffer as $row) {
                        $row['type'] = $type_list[$row['type']]['name'];
                        switch ($row['status']){
                            case 1:
                                $row['status'] = '发送成功';
                                break;
                            case 2:
                                $row['status'] = '发送失败';
                                break;
                            default:
                                $row['status'] = '未知状态';
                                break;
                        }
                        switch ($row['trade_status']){
                            case 'DELIVRD':
                                $row['trade_status'] = '到达成功';
                                break;
                            case 'UNKNOW':
                                $row['trade_status'] = '到达未知';
                                break;
                            default:
                                $row['trade_status'] = '到达失败';
                                break;
                        }

                        foreach ($keys as $key) {
                            $data[] = $row[$key];
                        }
                        if (isset($data)){
                            $user[] = implode("\t ,", $data) . "\t ,";
                            unset($data);
                        }
                    }
                    $html .= implode("\n", $user)."\n";
                    unset($user);
                }
            }
        }
        header("Content-type:text/csv");
        header("Content-Disposition:attachment; filename=礼品记录.csv");
        echo $html;
        exit();
    }

    $pager = pagination($page_total,$page_index,$page_size);
}

if ($ac=='reportSms'){
    Sms::getSmsLog($themeid);
}


$return_clicent = get_defined_vars();
return view('/sms', $return_clicent);
}
}